package com.leji.song.myOrder;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.dgl.sdk.util.SharePreStore;
import com.leji.song.R;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.extra.MyApplication;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.myOrder.model.DS_MyOrder;
import com.leji.song.util.ConstAns;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.util.List;

/**
 * Created by colin.ding on 2014/6/23.
 */
public class MyOrderAdapter extends BaseAdapter {
    protected ListItemView listItemView;
    protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_MyOrder> myOrderList;

    public interface IComment{
        void onComment(int pos);
    }

    IComment iComment;

    public MyOrderAdapter(){};

    public MyOrderAdapter(Context context, List<DS_MyOrder> myOrderList, IComment iComment){
        this.context = context;
        this.myOrderList = myOrderList;
        this.iComment = iComment;

        listContainer = LayoutInflater.from(context);
    }

    protected static class ListItemView{
        public TextView time;
        public TextView shopName;
        public TextView caiName;
        public TextView price;
        public TextView state;
        public Button comment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myOrderList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.my_order_adapter, null);
            // 获取控件对象
            listItemView.time = (TextView) convertView
                    .findViewById(R.id.time);
            listItemView.shopName = (TextView) convertView
                    .findViewById(R.id.shopName);
            listItemView.caiName = (TextView) convertView
                    .findViewById(R.id.caiName);
            listItemView.price = (TextView) convertView
                    .findViewById(R.id.price);
            listItemView.state = (TextView) convertView
                    .findViewById(R.id.state);

            listItemView.comment = (Button) convertView
                    .findViewById(R.id.comment);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        listItemView.time.setText(myOrderList.get(position).getTime());
        listItemView.shopName.setText(myOrderList.get(position).getShopsname());
        listItemView.caiName.setText(myOrderList.get(position).getGoodsname());
        listItemView.price.setText("¥" + myOrderList.get(position).getSumprice());

        if(myOrderList.get(position).getState().equals("已完成")){

            listItemView.state.setVisibility(View.GONE);
            listItemView.comment.setVisibility(View.VISIBLE);
            listItemView.comment.setTag(position);
            listItemView.comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (Integer)v.getTag();
                    iComment.onComment(pos);
                }
            });
        }else{
            listItemView.state.setVisibility(View.VISIBLE);
            listItemView.comment.setVisibility(View.GONE);
            listItemView.state.setText(myOrderList.get(position).getState());
        }

        convertView.setOnClickListener(new MyClickListener(position));

        return convertView;
    }


    class MyClickListener implements View.OnClickListener {
        private int position;

        public MyClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, MyOrderDetailActivity.class);
            intent.putExtra("orderId", myOrderList.get(position).getId());
            context.startActivity(intent);
        }

    }
}

